package com.gitee.liuzhi.framework.starter.response.base;

import org.springframework.web.bind.annotation.ModelAttribute;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by 2022/4/29/0029
 *
 * @author LiuZhi
 * @version V1.0
 * @description: BaseController
 */
public class BaseController {

    private static final ThreadLocal<HttpServletRequest> REQUEST_THREAD_LOCAL = new ThreadLocal<>();

    private static final ThreadLocal<HttpServletResponse> RESPONSE_THREAD_LOCAL = new ThreadLocal<>();

    @ModelAttribute
    public void setRequestAndResponse(HttpServletRequest request, HttpServletResponse response) {
        REQUEST_THREAD_LOCAL.remove();
        REQUEST_THREAD_LOCAL.set(request);
        RESPONSE_THREAD_LOCAL.remove();
        RESPONSE_THREAD_LOCAL.set(response);
    }

    public HttpServletRequest getRequest() {
        return REQUEST_THREAD_LOCAL.get();
    }

    public HttpServletResponse getResponse() {
        return RESPONSE_THREAD_LOCAL.get();
    }


}
